package com.wthink.hzgj.menu

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.view.menu.MenuAdapter
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.Navigation
import androidx.navigation.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.wthink.hzgj.BusApplication
import com.wthink.hzgj.R
import com.wthink.hzgj.databinding.FragmentMenuList2Binding
import com.wthink.hzgj.menu.adapter.MenuListAdapter
import com.wthink.hzgj.menu.viewmodel.MenuListViewModel

import com.wthink.hzgj.util.adapter.OnItemClickListener

class MenuListFragment2 : Fragment() {

    private val viewModel by viewModels<MenuListViewModel>()

    private lateinit var binding: FragmentMenuList2Binding

    private val loginModel by viewModels<LoginModel>()


    private val loginDialog by lazy {
        LoginDialog.make(
            BusApplication.busContext, binding.root as ViewGroup, loginModel,
            activity as AppCompatActivity
        )
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_menu_list2,
            container,
            false
        )
        binding.viewModel = viewModel
        binding.lifecycleOwner = this
        return binding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        //val nav = activity?.let { Navigation.findNavController(it, R.id.main) }
        //返回
        binding.back.setOnClickListener {
            it.findNavController().popBackStack()
        }

        viewModel.addList()

        binding.menuListView.layoutManager = GridLayoutManager(
            context,
            2, LinearLayoutManager.VERTICAL, false
        )

        binding.menuListView.adapter = MenuListAdapter().apply {
            mItemClickListener = object : OnItemClickListener {
                override fun onItemClick(obj: Any, position: Int) {
                    val item = obj as String
                    when (item) {
                        "主页"->{
                            Navigation.findNavController(binding.main).navigate(R.id.newHomeFragment)
                        }
                        "设置中心"->{
                            Navigation.findNavController(binding.main).navigate(R.id.newCameraFragment)
                        }
                        "设备状态"->{
                            Navigation.findNavController(binding.main).navigate(R.id.stateFragment)
                        }
                        "版本信息"->{
                            Navigation.findNavController(binding.main).navigate(R.id.verisonFragment)
                        }
                        "调度申请"->{
                            Navigation.findNavController(binding.main).navigate(R.id.businessFragment)
                        }
                        "存储介质"->{
                            Navigation.findNavController(binding.main).navigate(R.id.sdcardFragment)
                        }
                        "消息列表"->{
                            Navigation.findNavController(binding.main).navigate(R.id.msgListFragment)
                        }
                        "视频拷贝"->{
                            Navigation.findNavController(binding.main).navigate(R.id.copyFileCameraFragment)
                        }
                        "GPS信息"->{
                            Navigation.findNavController(binding.main).navigate(R.id.gpsinfoFragment)
                        }
                        "服务器配置"->{
                            Navigation.findNavController(binding.main).navigate(R.id.netConfigListFragment)
                        }
                        "无线网络信息"->{
                            Navigation.findNavController(binding.main).navigate(R.id.wirelessFragment)
                        }
                        "车辆信息"->{
                            Navigation.findNavController(binding.main).navigate(R.id.busInfoFragment)
                        }
                        "行车计划"->{
                            Navigation.findNavController(binding.main).navigate(R.id.busPlanListFragment)
                        }
                    }
                }
            }
        }
    }
}


